#!/bin/sh -e

function error_not_inside_sb2()
{
	echo "SB2: This script can only be used from inside"
	echo "the scratchbox 2'ed environment"
	exit 1
}

if [ -z "$SBOX_SESSION_DIR" ]
then
	error_not_inside_sb2
fi

. $SBOX_SESSION_DIR/sb2-session.conf

. ~/.scratchbox2/config

[ ! -z "$sbox_target" ] || sbox_target=$DEFAULT_TARGET

. ~/.scratchbox2/$sbox_target/sb2.config

# Read in primary toolchain config, if it has been defined;
# SBOX_CROSS_* variables are there nowadays
if [ -f $HOME/.scratchbox2/$sbox_target/sb2.config.d/gcc.config.sh ]; then
	. $HOME/.scratchbox2/$sbox_target/sb2.config.d/gcc.config.sh 
else
	echo "$HOME/.scratchbox2/$sbox_target/sb2.config.d/gcc.config.sh does not exist;"
	echo "Can't build qemuserver"
	exit 1
fi

# ------------
# FIXME: / LTA 2008-09-18:
# Export variables from sb2.config; These used to be exported from sb2.config,
# but the export statements were moved out from the config file.
# I'm not sure how many of these are really needed by this script.

export SBOX_CONFIG_VERSION

export DEB_BUILD_ARCH
export DEB_BUILD_ARCH_CPU
export DEB_BUILD_ARCH_ABI
export DEB_BUILD_GNU_CPU
export DEB_BUILD_GNU_TYPE
export DEB_BUILD_GNU_SYSTEM

export DEB_HOST_ARCH
export DEB_HOST_ARCH_OS
export DEB_HOST_ARCH_CPU

export DEB_HOST_GNU_CPU
export DEB_HOST_GNU_TYPE
export DEB_HOST_GNU_SYSTEM

export LD_LIBRARY_PATH
# ------------

export COMPILER=$SBOX_CROSS_GCC_DIR/$SBOX_CROSS_GCC_SUBST_PREFIX

# uh evil hack sb2 need --copy-clibrary
for linker in $(find $SBOX_CROSS_GCC_DIR/.. -name 'ld-linux.so*'); do
	export LIBCDIR=`dirname $linker`
done

if [ -z "$LIBCDIR" ]; then
	echo "could not find libc!"
	exit 2;
fi

[ ! -z "$K_COMPILER" ] || export K_COMPILER=$COMPILER

rm -rf builddir/*
mkdir -p builddir/.downloads

cd builddir/.downloads

[ -r busybox-1.9.0.tar.bz2 ] || wget http://www.busybox.net/downloads/busybox-1.9.0.tar.bz2
[ -r linux-2.6.23.13.tar.bz2 ] || wget http://ftp.fi.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.13.tar.bz2
[ -r sbrsh_7.6.tar.gz ] || wget http://ftp.fi.debian.org/debian/pool/main/s/sbrsh/sbrsh_7.6.tar.gz

cd ..

tar xjf .downloads/busybox-1.9.0.tar.bz2
tar xjf .downloads/linux-2.6.23.13.tar.bz2
tar xzf .downloads/sbrsh_7.6.tar.gz

cd linux-2.6*

if [ -r ../.downloads/.config ]; then
	cp ../.downloads/.config .
	ARCH=arm CROSS_COMPILE=$K_COMPILER make oldconfig
else
	ARCH=arm CROSS_COMPILE=$K_COMPILER make realview_defconfig
	echo make sure you have initrd support
	echo add eabi support
	echo add tmpfs support
	echo add loopback mount support
	echo ok? - press enter to continue
	read
	ARCH=arm CROSS_COMPILE=$K_COMPILER make menuconfig
	cp .config ../.downloads/
fi
ARCH=arm CROSS_COMPILE=$K_COMPILER make
cp arch/arm/boot/zImage ..

cd ..

cd busybox-*
	ARCH=arm CROSS_COMPILE=$COMPILER make defconfig
	ARCH=arm CROSS_COMPILE=$COMPILER make
	ARCH=arm CROSS_COMPILE=$COMPILER make install
	cp -a _install/ ../initramfs
cd ..


cd sbrsh-*
	make CC=${COMPILER}gcc
cd ..

mkdir -p initramfs/{boot,etc,proc,sys,mnt,sbin,dev,lib,usr/bin,tmp}
cp $LIBCDIR/* initramfs/lib/
cp sbrsh-7.6/optimize/bin/sbrshd initramfs/sbin/
echo '*' > initramfs/etc/sbrshd.conf
cat > initramfs/init <<EOF
#!/bin/sh
echo "Loading, please wait..."
mount -t proc proc /proc
mount -t sysfs sysfs /sys
ifconfig eth0 10.0.2.15 netmask 255.255.255.0 up
ifconfig lo 127.0.0.1 netmask 255.0.0.0 up
route add default gw 10.0.2.2
# The daily WTF: qemu enables networking only after guest
# communicates to you
ping -c 3 10.0.2.2
# The daily WTF #2: binmounts fail if mount source dir
# is initramfs
cp -a /dev /dev2
mount -t ramfs ramfs /dev
mv /dev2/* /dev
mount -t devpts devpts /dev/pts
mount -t sysfs sysfs /sys
mount -t ramfs ramfs /tmp
chmod a+rwx /tmp
/sbin/sbrshd

/bin/sh </dev/console >/dev/console
EOF
chmod a+x initramfs/init
chmod a+x initramfs/lib/ld-linux*

fakeroot sb2-mkinitramfs initramfs initramfs.gz

echo Add following in your /etc/exports
echo
echo "$HOME 127.0.0.1(rw,all_squash,anonuid=$UID,anongid=$UID,sync,insecure)"
echo
echo for sbox target config:
echo
echo "sb2-init -sn -r 127.0.0.1 -l 10.0.2.2 -c sbrsh MAEMO $COMPILER"
